import tool
import matplotlib.pyplot as plt
import numpy as np
import os
import mapping

plt.rcParams["font.sans-serif"] = ["SimHei"]  # 使用黑体
plt.rcParams["axes.unicode_minus"] = False  # 解决负号显示问题

dir_path = "c:/Users/16028/Desktop/mcm/2023c/proj/picture"


def show_or_print(file_name=None):
    if file_name:
        file_path = os.path.join(dir_path, file_name)
        plt.savefig(file_path, bbox_inches="tight", dpi=300)
    else:
        plt.show()


def long_tail(file_name=None):
    data = tool.get_np("product_total_quantity.csv")
    # 绘制长尾分布图
    plt.figure(figsize=(10, 6))
    plt.plot(
        range(1, np.max(data["code"] + 1)),
        np.sort(data["total_quantity"])[::-1],
        marker="o",
        color="orange",
        markersize=1,
    )
    plt.xscale("linear")
    plt.yscale("log")  # 使用对数刻度显示长尾特征
    plt.title("出售量的长尾分布图")
    plt.xlabel("")
    plt.ylabel("销售量")
    plt.grid(True)
    show_or_print(file_name)


def type_monthly_quantity(file_name=None):

    plt.figure(figsize=(15, 6))
    names = list(mapping.type_map1.values())
    for i in range(1, 7):
        data = tool.get_np(f"type_monthly_total/{i}.csv")
        plt.plot(
            data["month"],
            data["monthly_total"],
            marker="o",
            label=names[i - 1],
            markersize=3,
        )
    plt.title("各类商品每月销售总量")
    plt.xlabel("月份")
    plt.ylabel("销量")
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.legend()
    show_or_print(file_name)


def Ga_picture():
    data_list = [
        (5, 0.7536241),
        (7, 0.5446938),
        (10, 0.6889245),
        (16, 0.6494169),
        (26, 0.6465373),
        (27, 0.7862371),
        (40, 0.7045043),
        (51, 0.9224251),
        (58, 0.3377026),
        (59, 0.4892075),
        (61, 0.4089075),
    ]

    # 将列表中的元组拆分为两个列表，一个用于x轴，一个用于y轴
    codes, additions = zip(*data_list)

    # 绘制散点图
    plt.figure(figsize=(10, 6))
    plt.scatter(codes, additions, color="blue", marker="o")

    # 添加标题和轴标签
    plt.title("Scatter Plot of Code vs. Addition")
    plt.xlabel("Code")
    plt.ylabel("Addition")

    # 显示网格
    plt.grid(True)

    # 显示图像
    plt.show()


if __name__ == "__main__":
    Ga_picture()
